#include "stdio.h"
#include "opencv/cv.h"
#include "opencv/highgui.h"

int tmain(int argc, char **argv) {
  IplImage *img = 0;

  if (argc < 2) {
      fprintf(stderr, "Usage: loading image <filename>\n");
      return 1;
  }

  img = cvLoadImage(argv[1], CV_LOAD_IMAGE_COLOR);
  if( img == 0 ) {
      fprintf( stderr, "Cannot load file %s!\n", argv[1] );
      return 1;
  }

  /* convert to gray scale */
  int width  = img->width;
  int height = img->height;
  IplImage *dst = cvCreateImage( cvSize( width, height ), IPL_DEPTH_8U, 1 );
  cvCvtColor( img, dst, CV_RGB2GRAY );

  cvReleaseImage( &img );

  /* create a window */
  cvNamedWindow( "image", CV_WINDOW_AUTOSIZE );

  /* display the image */
  cvShowImage( "image", dst );

  /* wait until user press a key */
  cvWaitKey(0);

  /* free memory */
  cvDestroyWindow( "image" );
  cvReleaseImage( &dst );

  return 0;
}
